package com.billProvider.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.billProvider.entity.FinaBillEcharts;
import com.billProvider.mapper.FinaBillMapper;
import com.billProvider.utils.GetDateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Bill;
import entity.ResponseMessage;
import entity.StatusCode;
import entity.Storebill;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.financial.FinaBillService;
import util.CropStringUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author oahner
 * @create 2022-04-18 10:13
 */
@Service
public class FinaBillServiceImpl implements FinaBillService {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private FinaBillMapper finaBillMapper;


    /**
     * 获取最近7天的财务账单数据（折线图）
     * @param
     * @return
     */
    @Override
    public ResponseMessage getFinaBillInLastW() {
        logger.info("FinaBillServiceImpl getFinaBillInLastW init===================");
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        try {
            //获取最近7天的日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ArrayList<String> days = GetDateUtil.getDays(7);
            //渲染折线图（账单数据，最近7天日期）
            Map<String,List> billMap = new HashMap<>();
            billMap.put("date",days);
            List<Object> billEcharts = new ArrayList<>();
            //折线图渲染数据对象（收入）
            FinaBillEcharts sbillEcharts = new FinaBillEcharts();
            List<String> slist = new ArrayList<>();
            sbillEcharts.setName("收入");
            //折线图渲染数据对象（支出）
            FinaBillEcharts zbillEcharts = new FinaBillEcharts();
            List<String> zlist = new ArrayList<>();
            zbillEcharts.setName("支出");
            //从数据库获取最近7天的数据
            List<Bill> finaBills = finaBillMapper.selFinaBillInLastW();
            //循环匹配日期，放入对应日期和状态的数据
            for(int i = 0;i<days.size();i++){
                for (Bill finaBill : finaBills) {
                    if(null!=finaBill && finaBill.getStatus() == 0 && sdf.format(finaBill.getCreateTime()).equals(days.get(i))){
                        zlist.add(i,finaBill.getMoney().toString());

                    }else if(null!=finaBill && finaBill.getStatus() == 1 && sdf.format(finaBill.getCreateTime()).equals(days.get(i))){
                        slist.add(i,finaBill.getMoney().toString());
                    }
                }
                if(zlist.size()<i+1 || zlist.size()==0){
                    zlist.add(i,"0");
                }
                if(slist.size()<i+1 || slist.size()==0){
                    slist.add(i,"0");
                }
            }
            //结合日期和状态，封装数据值billMap
            sbillEcharts.setData(slist);
            zbillEcharts.setData(zlist);
            billEcharts.add(sbillEcharts);
            billEcharts.add(zbillEcharts);
            billMap.put("data",billEcharts);
            //封装数据进ResponseMessage对象
            msg.setFlag(true);
            msg.setCode(StatusCode.OK);
            msg.setMessage("select billInLastW success");
            msg.setData(billMap);
            logger.debug("FinaBillServiceImpl getFinaBillInLastW  result:msg=" + msg);
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("select billInLastW is error");
            logger.error("FinaBillServiceImpl getFinaBillInLastW error ",e);
        }
        return msg;
    }

    /**
     * 获取财务账单列表
     * @param billPage
     * @return
     */
    @Override
    public ResponseMessage getFinaBill(Map<String,Object> billPage) {
        logger.info("FinaBillServiceImpl getFinaBill params:billPage="+billPage);
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        Bill bill = null;
        Integer pageNum = 1;
        if(null!=billPage.get("bill")){
            bill = JSONObject.parseObject(JSONObject.toJSONString(billPage.get("bill")), Bill.class);
            bill = (Bill) CropStringUtil.cropString(bill);
            logger.debug("FinaBillServiceImpl getFinaBill cropString:bill="+bill);
        }
        if(null!=billPage.get("pageNum") && (Integer)billPage.get("pageNum")>0){
            pageNum = (Integer) billPage.get("pageNum");
        }
        try {
            //从数据库获取财务账单列表
            PageHelper.startPage(pageNum,5);
            List<Bill> bills = finaBillMapper.selFinaBill(bill);
            //判断是否有数据
            if (null!=bills && bills.size() > 0) {
                PageInfo<Bill> pageInfo = new PageInfo<Bill>(bills);
                //封装数据进ResponseMessage对象
                msg.setFlag(true);
                msg.setCode(StatusCode.OK);
                msg.setMessage("select bill success");
                msg.setData(pageInfo);
                logger.debug("FinaBillServiceImpl getFinaBill  result:msg=" + msg);
            } else {
                msg.setMessage("select bill empty");
                logger.debug("FinaBillServiceImpl getFinaBill  params:bill=" + bill);
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("select bill is error");
            logger.error("FinaBillServiceImpl getFinaBill error parmas:bill="+bill,e);
        }
        return msg;
    }

    /**
     * 获取上月的财务账单数据（饼图）
     * @param
     * @return
     */
    @Override
    public ResponseMessage getFinaBillInLastM() {
        logger.info("FinaBillServiceImpl getFinaBillInLastM init===================");
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        try {
            //获取上月财务账单数据
            List<FinaBillEcharts> billsEcharts = finaBillMapper.selFinaBillInLastM();
            if (null!=billsEcharts && billsEcharts.size() > 0) {
                msg.setFlag(true);
                msg.setCode(StatusCode.OK);
                msg.setMessage("select billInLastM success");
                msg.setData(billsEcharts);
                logger.debug("FinaBillServiceImpl getFinaBillInLastM if(!null) result:msg=" + msg);
            } else {
                msg.setMessage("select billInLastM is empty");
                logger.debug("FinaBillServiceImpl getFinaBillInLastM if(null) ");
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("select billInLastM is error");
            logger.error("FinaBillServiceImpl getFinaBillInLastM error ",e);
        }
        return msg;
    }

    /**
     * 获取去年的财务账单数据（饼图）
     * @param
     * @return
     */
    @Override
    public ResponseMessage getFinaBillInLastY() {
        logger.info("FinaBillServiceImpl getFinaBillInLastY init===================");
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        try {
            //获取去年财务账单数据
            List<FinaBillEcharts> billsEcharts = finaBillMapper.selFinaBillInLastY();
            if (null!=billsEcharts && billsEcharts.size() > 0) {
                msg.setFlag(true);
                msg.setCode(StatusCode.OK);
                msg.setMessage("select billInLastY success");
                msg.setData(billsEcharts);
                logger.debug("FinaBillServiceImpl getFinaBillInLastY if(!null) result:msg=" + msg);
            } else {
                msg.setMessage("select billInLastY is empty");
                logger.debug("FinaBillServiceImpl getFinaBillInLastY if(null) "  );
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("select billInLastY is error");
            logger.error("FinaBillServiceImpl getFinaBillInLastY error ",e);
        }
        return msg;
    }
}
